<?php
/**
 * @brief Error include file.
 *
 * @license		GNU Public License
 * @copyright	2008 David Singer
 * @author		David Singer <david@ramaboo.com>
 * @version		1.0.0
 */

function exception_handler($e) {
	// display uncaught excpetion
	echo "<h1>Uncaught Exception</h1>";
	echo "<ul>";
	echo "<li><b>Message:</b> " . $e->getMessage() . "</li>";
	echo "<li><b>Code:</b> " . $e->getCode() . "</li>";
	echo "<li><b>File:</b> " . $e->getFile() . "</li>";
	echo "<li><b>Line:</b> " . $e->getLine() . "</li>";
	echo "<li><b>Trace:</b> " . $e->getTraceAsString() . "</li>";
	echo "</ul>";
	
	// log error
	$content = array();
	$content['date'] = date('c', time());
	$content['message'] = $e->getMessage();
	$content['code'] = $e->getCode();
	$content['file'] = $e->getFile();
	$content['line'] = $e->getLine();
	$content['trace'] = $e->getTraceAsString();
	
	try {
		write_log($content);
	} catch (Exception $e) {
		// this prevents a stack frame error if write_log throws an exception
		exit($e->getMessage());
	}
	
	exit;
}

function error_handler($error, $error_string, $file, $line, $symbols) {
	// setup human friendly error name
	switch ($error) {
		case E_ERROR:
			$error_name = 'E_ERROR';
			break;
			
		case E_WARNING:
			$error_name = 'E_WARNING';
			break;
			
		case E_PARSE:
			$error_name = 'E_PARSE';
			break;
			
		case E_NOTICE:
			$error_name = 'E_NOTICE';
			break;
			
		case E_USER_ERROR:
			$error_name = 'E_USER_ERROR';
			break;
			
		case E_USER_WARNING:
			$error_name = 'E_USER_WARNING';
			break;
			
		case E_USER_NOTICE:
			$error_name = 'E_USER_NOTICE';
			break;
			
		case E_ALL:
			$error_name = 'E_ALL';
			break;	
			
		default: // unknown error
			$error_name = 'UNKNOWN';
	}
	
	// display error
	echo "<h1>Error</h1>";
	echo "<ul>";
	echo "<li><b>Message:</b> $error_string</li>";
	echo "<li><b>Code:</b> $error ($error_name)</li>";
	echo "<li><b>File:</b> $file</li>";
	echo "<li><b>Line:</b> $line</li>";
	echo "<li><b>Symbols:</b> <small>" . serialize($symbols) . "</small></li>";
	echo "</ul>";
	
	// log error
	$content = array();
	$content['date'] = date('c', time());
	$content['error_string'] = $error_string;
	$content['code'] = $error;
	$content['file'] = $file;
	$content['line'] = $line;
	
	$content['symbols'] = serialize($symbols);
	
	write_log($content);
	
	exit;
}

// set handlers
set_error_handler('error_handler');
set_exception_handler('exception_handler');
?>
